package com.darker.yygh.receiver;

import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.order.OrderMqVo;
import com.darker.common.rabbit.constant.MqConst;
import com.darker.common.rabbit.service.RabbitService;
import com.darker.yygh.order.client.OrderFeignClient;
import com.darker.yygh.service.ScheduleService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class HospitalReceiver {

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private RabbitService rabbitService;

    @RabbitListener( bindings = @QueueBinding(
            value = @Queue(value = MqConst.ORDER_DEAD_QUEUE),
            exchange = @Exchange(value = MqConst.ORDER_DEAD_EXCHANGE),
            key = MqConst.ORDER_DEAD_KEY
    ))
    public void isOrderCancel(Long orderId){
        int orderState = orderFeignClient.getOderById(orderId);
        if(orderState== OrderStatusEnum.UNPAID.getStatus()){
            orderFeignClient.cancelOrder(orderId);

        }

    }



    /**
     * description: 更新排班的预约号
     * @param orderMqVo:
     * @return: void<br>
     * date: 2022/12/12 16:38<br>
     * author: Darker<br>
     * version: 1.0
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = MqConst.CANCEL_ORDER),
                    exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_ORDER),
                    key = {MqConst.CANCEL_ORDER}
            )
    )
    public void cancelOrder(OrderMqVo orderMqVo){
        //取消预约更新预约数

        Schedule schedule = scheduleService.getScheduleId(orderMqVo.getScheduleId());
        int availableNumber = schedule.getAvailableNumber().intValue() + 1;
        schedule.setAvailableNumber(availableNumber);
        scheduleService.update(schedule);
    }

/**
 * description: 生成订单
 * @param orderMqVo:
 * @param message:
 * @param channel:
 * @return: void<br>
 * date: 2022/12/12 16:38<br>
 * author: Darker<br>
 * version: 1.0
 */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_ORDER, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_ORDER),
            key = {MqConst.ROUTING_ORDER}
    ))
    public void receiver(OrderMqVo orderMqVo, Message message, Channel channel) throws IOException {
        if (null!=orderMqVo) {
            //下单成功更新预约数
            Schedule schedule = scheduleService.getScheduleId(orderMqVo.getScheduleId());
            schedule.setReservedNumber(orderMqVo.getReservedNumber());
            schedule.setAvailableNumber(orderMqVo.getAvailableNumber());
            scheduleService.update(schedule);
        }
//        else {
//            //取消预约更新预约数
//            Schedule schedule = scheduleService.getScheduleId(orderMqVo.getScheduleId());
//            int availableNumber = schedule.getAvailableNumber().intValue() + 1;
//            schedule.setAvailableNumber(availableNumber);
//            scheduleService.update(schedule);
//        }
//        //发送短信
//        MsmVo msmVo = orderMqVo.getMsmVo();
//        if (null != msmVo) {
//            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_MSM, MqConst.ROUTING_MSM_ITEM, msmVo);
//        }
    }
}
